package com.dream.technology.service.impl;


import com.dream.technology.components.riskControl.tianji.RcConstan;
import com.dream.technology.db.dao.RcCustomerTagItemMapper;
import com.dream.technology.db.dao.RcCustomerTagMapper;
import com.dream.technology.db.entity.RcCustomerTag;
import com.dream.technology.db.entity.RcCustomerTagItem;
import com.dream.technology.db.entity.RcCustomerTagItemQuery;
import com.dream.technology.service.RcCustomerTagItemService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("rcCustomerTagItemService")
public class RcCustomerTagItemServiceImpl implements RcCustomerTagItemService {

    @Autowired
    RcCustomerTagItemMapper rcCustomerTagItemMapper;

    @Autowired
    RcCustomerTagMapper rcCustomerTagMapper;

    @Override
    public int countByExample(RcCustomerTagItemQuery example) {
        return rcCustomerTagItemMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(RcCustomerTagItemQuery example) {
        return rcCustomerTagItemMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return rcCustomerTagItemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(RcCustomerTagItem record) {
        return rcCustomerTagItemMapper.insert(record);
    }

    @Override
    public int insertList(List<RcCustomerTagItem> tagItemList) {
        return rcCustomerTagItemMapper.insertList(tagItemList);
    }

    @Override
    public int insertSelective(RcCustomerTagItem record) {
        return rcCustomerTagItemMapper.insertSelective(record);
    }

    @Override
    public List<RcCustomerTagItem> selectByExample(RcCustomerTagItemQuery example) {
        return rcCustomerTagItemMapper.selectByExample(example);
    }

    @Override
    public RcCustomerTagItem selectByPrimaryKey(Long id) {
        return rcCustomerTagItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(RcCustomerTagItem record, RcCustomerTagItemQuery example) {
        return rcCustomerTagItemMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByExample(RcCustomerTagItem record, RcCustomerTagItemQuery example) {
        return rcCustomerTagItemMapper.updateByExample(record, example);
    }

    @Override
    public int updateByPrimaryKeySelective(RcCustomerTagItem record) {
        return rcCustomerTagItemMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(RcCustomerTagItem record) {
        return rcCustomerTagItemMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<RcCustomerTag> queryCustomerTagByItem(Long customerTagItem) {
        return rcCustomerTagMapper.queryCustomerTagByItem(0, customerTagItem);
    }

    /**
     * 根据标签id 复制标签的配置项
     *
     * @param tagId    被复制的tagId
     * @param newTagId 复制出的tagId
     * @return
     */
    @Override
    public String copy(Long tagId, Long newTagId) {
        List<RcCustomerTagItem> rcCustomerTagItems = selectByTagId(tagId);
        if (CollectionUtils.isEmpty(rcCustomerTagItems)) {
        	return RcConstan.SUCCESS;
        }
        ArrayList<RcCustomerTagItem> tagItems = new ArrayList<>();
        rcCustomerTagItems.forEach(item -> {
            item.setId(null);
            item.setTagId(newTagId);
            tagItems.add(item);
        });
        rcCustomerTagItemMapper.insertList(tagItems);

        return RcConstan.SUCCESS;
    }

    /**
     * 查询指定taiId下的所有配置
     *
     * @param tagId
     * @return
     */
    @Override
    public List<RcCustomerTagItem> selectByTagId(Long tagId) {
        RcCustomerTagItemQuery query = new RcCustomerTagItemQuery();
        RcCustomerTagItemQuery.Criteria criteria = query.createCriteria();
        criteria.andTagIdEqualTo(tagId);
        List<RcCustomerTagItem> rcCustomerTagItems = rcCustomerTagItemMapper.selectByExample(query);
        return rcCustomerTagItems;
    }

 /**
     * 查询指定taiId下的所有配置
     *
     * @param tagId
     * @return
     */
    @Override
    public List<RcCustomerTagItem> selectByTagIds(List<Long> tagId) {
        RcCustomerTagItemQuery query = new RcCustomerTagItemQuery();
        RcCustomerTagItemQuery.Criteria criteria = query.createCriteria();
        criteria.andTagIdIn(tagId);
        List<RcCustomerTagItem> rcCustomerTagItems = rcCustomerTagItemMapper.selectByExample(query);
        return rcCustomerTagItems;
    }


}
